//
//  ModuleFirstRootViewController.m
//  艾听歌
//
//  Created by dllo on 16/5/28.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TravelPenViewController.h"
#import "SpecialTopicViewController.h"
#import "FirstTravelsController.h"


static CGFloat const titleH = 44;
static CGFloat const navBarH = 64;
//static CGFloat const maxTitleScale = 1.2;


#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH  [UIScreen mainScreen].bounds.size.height


@interface TravelPenViewController ()<UIScrollViewDelegate>

@property (strong, nonatomic) SpecialTopicViewController *MFTopicVC;

@property (strong, nonatomic) FirstTravelsController *MFTravelsVC;

@property (nonatomic, strong) UIScrollView *titleScrollView;

@property (nonatomic, strong) UIScrollView *contentScrollView;

//选中按钮
@property (nonatomic, strong) UIButton *selTitleButton;

@property (nonatomic, strong) NSMutableArray *buttons;

@end

@implementation TravelPenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"游记";
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0xFF6EB4, 0x9C9C9C);
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    


    
    self.buttons = [NSMutableArray array];
    
    //1.创建小scrollView
    [self setupTitleScrollView];
    
    //2.创建大scrollView
    [self setupContentScrollView];
    
    //3.给打scrollView设置内容VC
    [self addChildViewController];
    
    //4.布局按钮标题
    [self setupTitle];
    
    
    self.contentScrollView.contentSize = CGSizeMake(self.childViewControllers.count * ScreenW, 0);
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.delegate = self;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
  }




//设置头部标题
- (void)setupTitleScrollView
{
    //判断是否存在导航控制器来判断y值
    CGFloat y = self.navigationController ? navBarH : 0;
    CGRect rect = CGRectMake(0, y, ScreenW, titleH);
    
   self.titleScrollView = [[UIScrollView alloc]initWithFrame:rect];
    self.titleScrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.titleScrollView];
    
    self.titleScrollView = self.titleScrollView;
    
}


#pragma mark -- 设置大scrollView
- (void)setupContentScrollView
{
    CGFloat y =    CGRectGetMaxY(self.titleScrollView.frame);
    CGRect rect = CGRectMake(0, y, ScreenW, ScreenH);
    
    self.contentScrollView = [[UIScrollView alloc]initWithFrame:rect];
    [self.view addSubview:self.contentScrollView];
    
    self.contentScrollView = self.contentScrollView;
    
}


#pragma mark -- 添加子控制器
- (void)addChildViewController{
    
   //将FirstTravelsVC放进容器视图控制器
    self.MFTravelsVC = [[FirstTravelsController alloc]init];
    self.MFTravelsVC.title = @"游记";
    [self addChildViewController:self.MFTravelsVC];
    
    
    
//    将FirstTopicVC放进容器视图控制器
    self.MFTopicVC = [[SpecialTopicViewController alloc]init];
    self.MFTopicVC.title = @"专题";
    [self addChildViewController:self.MFTopicVC];
    

}


//布局按钮坐标
- (void)setupTitle{
    
    NSUInteger count =  self.childViewControllers.count;
    
    
    CGFloat x = 0;
    CGFloat w = (ScreenW - 20) / 2;
    CGFloat h = (titleH - 10) ;
    
    for (int i = 0; i < count; i++) {
        
        UIViewController *VC = self.childViewControllers[i];
        
        x = i * w;
        CGRect rect = CGRectMake(10 + x, 5, w, h);
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = rect;
        
        
        btn.tag = i;
        [btn setTitle:VC.title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        btn.backgroundColor = [UIColor whiteColor];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 5;
        
        [btn addTarget:self action:@selector(chick:) forControlEvents:UIControlEventTouchDown];
        
        [self.buttons addObject:btn];
        [self.titleScrollView addSubview:btn];
        
        //设置默认第一个被选中
        if (i == 0) {
            
            [self chick:btn];
        }
        
        
    }
    
    self.titleScrollView.contentSize = CGSizeMake(count * w, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
}


// 按钮点击
- (void)chick:(UIButton *)btn
{
    // 设置标题颜色为选中颜色
    [self selTitleBtn:btn];
    
    NSUInteger i = btn.tag;
    CGFloat x = i * ScreenW;
    
    [self setUpOneChildViewController:i];
    
    // 点击按钮切换对应的vc
    self.contentScrollView.contentOffset = CGPointMake(x, 0);
    
}

// 选中按钮
- (void)selTitleBtn:(UIButton *)btn
{
    // 先还原上次选中按钮颜色和大小
    [self.selTitleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.selTitleButton setBackgroundColor:[UIColor whiteColor]];
//    self.selTitleButton.transform = CGAffineTransformIdentity;
    // 设置当前选中的
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor colorWithRed:248 / 255.0 green:110 / 255.0 blue:142 / 255.0 alpha:0.8]];
    //    btn.transform = CGAffineTransformMakeScale(maxTitleScale, maxTitleScale);
    
    self.selTitleButton = btn;
    
    // 设置选中按钮保持在中心点附近位置
//    [self setupTitleCenter:btn];
}

- (void)setupTitleCenter:(UIButton *)btn
{
    CGFloat offset = btn.center.x - ScreenW * 0.5;
    
    // 如果偏移量小于0,说明按钮的中心点是在屏幕中心点的左侧,所以scrollview就不用偏移,保持偏移量为0就行.
    // 如果按钮中心点大于屏幕中心点,那么scrollview就偏移二者的差距就行.
    if (offset < 0)
    {
        offset = 0;
    }
    
    // scrollview的内容宽度-屏幕宽度,就是最大偏移量.也就是scrollview偏移最大偏移量就能显示最后的按钮了.
    CGFloat maxOffset = self.titleScrollView.contentSize.width - ScreenW;
    if (offset > maxOffset)
    {
        offset = maxOffset;
    }
    
    [self.titleScrollView setContentOffset:CGPointMake(offset, 0) animated:YES];
    
    
}


//滑动到某个位置就加载相应的位置的vc,不多余创建
- (void)setUpOneChildViewController:(NSUInteger)i
{
    CGFloat x = i * ScreenW;
    
    UIViewController *vc = self.childViewControllers[i];
    
    //如果当前vc上有子视图就不再添加
    if (vc.view.superview) {
        return;
    }
    
    // 只是将vc的view加在大scrollview上
    vc.view.frame = CGRectMake(x, 0, ScreenW, ScreenH - self.contentScrollView.frame.origin.y);
    
    [self.contentScrollView addSubview:vc.view];
    
}
#pragma mark - UIScrollViewDelegate
// 滑动scrollview 改变按钮和vc
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 滑动scrollview得到当前下标
    NSUInteger i = self.contentScrollView.contentOffset.x / ScreenW;
    // 更新选中按钮的颜色和大小
    [self selTitleBtn:self.buttons[i]];
    // 切换vc
    [self setUpOneChildViewController:i];
}

// 只要滚动UIScrollView就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    // 主要实现滑动中的缩放效果
    
    CGFloat offsetX = scrollView.contentOffset.x;
    // 左边按钮就是当前选中按钮
    NSInteger leftIndex = offsetX / ScreenW;
    NSInteger rightIndex = leftIndex + 1;
    
    //    NSLog(@"%zd,%zd",leftIndex,rightIndex);
    
    UIButton *leftButton = self.buttons[leftIndex];
    
    UIButton *rightButton = nil;
    if (rightIndex < self.buttons.count) {
        rightButton = self.buttons[rightIndex];
    }
    
    // offsetX / ScreenW = 当前下标左右的范围值 比如当前leftIndex为2 那么offsetX / ScreenW就是1.1~2.9的范围内  所以scaleR比例永远为0~0.9
    CGFloat scaleR = offsetX / ScreenW - leftIndex;
    // scaleL的值就为0.9~0
    CGFloat scaleL = 1 - scaleR;
    
    
    // 设置所有按钮的缩放范围都是1~1.3
//    CGFloat transScale = maxTitleScale - 1;
    // 0~1范围的数*0.3永远是0~0.3的范围
//    leftButton.transform = CGAffineTransformMakeScale(scaleL * transScale + 1, scaleL * transScale + 1);
    
//    rightButton.transform = CGAffineTransformMakeScale(scaleR * transScale + 1, scaleR * transScale + 1);
    
//    NSLog(@"L== %f  R== %f",scaleL  ,scaleR);
    
    
    UIColor *rightColor = [UIColor colorWithRed:scaleR green:0 blue:0 alpha:1];
    UIColor *leftColor = [UIColor colorWithRed:scaleL green:0 blue:0 alpha:1];
    
    [leftButton setTitleColor:leftColor forState:UIControlStateNormal];
    [rightButton setTitleColor:rightColor forState:UIControlStateNormal];
    
    
}


@end
